/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.sonrisas.tools.view;

import Complementos.Conexion_BD;
import Complementos.Sesion;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

/**
 *
 * @author DIEGOALV
 */
public class frmUsuario_1 extends javax.swing.JFrame {
Conexion_BD co=new Conexion_BD();
    public frmUsuario_1() {
        initComponents();
        this.setIconImage(new ImageIcon(getClass().getResource("/Media/logo1.png")).getImage());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtLogin = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        lblUsuario = new javax.swing.JLabel();
        lblPerfil = new javax.swing.JLabel();
        lblIP = new javax.swing.JLabel();
        lblSO = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 204, 240));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Final Soft Aplicación", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 102))); // NOI18N

        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        lblUsuario.setFont(new java.awt.Font("Verdana", 1, 13)); // NOI18N
        lblUsuario.setForeground(new java.awt.Color(255, 255, 153));
        lblUsuario.setText("usuario");

        lblPerfil.setFont(new java.awt.Font("Verdana", 1, 13)); // NOI18N
        lblPerfil.setForeground(new java.awt.Color(255, 255, 153));
        lblPerfil.setText("perfil");

        lblIP.setFont(new java.awt.Font("Verdana", 1, 13)); // NOI18N
        lblIP.setForeground(new java.awt.Color(255, 255, 102));
        lblIP.setText("ip");

        lblSO.setFont(new java.awt.Font("Trebuchet MS", 3, 14)); // NOI18N
        lblSO.setForeground(new java.awt.Color(255, 255, 102));
        lblSO.setText("S.O:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("CAMBIENME DE POSICION Y COLORES");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblSO)
                    .addComponent(lblIP)
                    .addComponent(lblPerfil)
                    .addComponent(lblUsuario)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jButton1)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(125, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(47, 47, 47))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(lblUsuario)
                        .addGap(18, 18, 18)
                        .addComponent(lblPerfil)
                        .addGap(32, 32, 32)
                        .addComponent(lblIP)
                        .addGap(29, 29, 29)
                        .addComponent(lblSO))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addComponent(jLabel1)))
                .addGap(54, 54, 54)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (login()){
            lblUsuario.setText("Bienvenido/a a Sonrisas Tools, "+Sesion.NOMBRE_USUARIO);
        lblPerfil.setText("Usted tiene privilegios de "+Sesion.NOMBRE_PERFIL);
        lblSO.setText(""+ System.getProperty("os.name"));
        try {
            lblIP.setText("Su dirección IP: "+ InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    
    
    public boolean login(){
        boolean login = false;    
        
        try {
            
            
            
            co.Conectar();
           ResultSet rs = null;
           String query = "SELECT count(usuario),u.idUsuario,u.usuario,p.nombres,pe.nombre "
                   + "FROM usuario u,persona p,perfil pe  "
                   + "WHERE usuario = '"+txtLogin.getText()+"'"+" "
                   + "AND u.idPERSONA = p.idPERSONA "
                   + "AND u.idPerfil = pe.id";
           
           
           rs = co.Consultar(query);       
           rs.next();
        if ( Integer.parseInt(rs.getString(1)) == 1){
                Sesion.ID_PERFIL        = rs.getInt("idUsuario");
                Sesion.LOGIN            = rs.getString("usuario");
                Sesion.NOMBRE_USUARIO   = rs.getString("p.nombres");
                Sesion.NOMBRE_PERFIL    = rs.getString("pe.nombre");
                //obtenerPrivilegiosDeUsuario(Sesion.ID_PERFIL);
                login = true;
                rs.close();
           }   
           else
           {
                //lblMensaje.setText("Usuario / Password incorrecto");
                //lblMensaje.setVisible(true);
                rs.close();
           }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(frmLogin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(frmLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        finally{
            try {
                co.Desconectar();
                
            } catch (Exception ex) {
                Logger.getLogger(frmLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
                
            
            }
        
        return login;
 
    }
    /**
     * 
     * 
     * 
     * 
     * @param args the command line arguments
     */
    public static void main(String args[]) {
                java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmUsuario_1().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblIP;
    private javax.swing.JLabel lblPerfil;
    private javax.swing.JLabel lblSO;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JTextField txtLogin;
    // End of variables declaration//GEN-END:variables
}
